package com.app.qbake.module.mine.adapter.delegate;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wind.base.adapter.BaseAdapterDelegate;
import com.wind.base.adapter.DisplayItem;
import com.wind.base.utils.ViewInflater;
import com.wind.data.mine.bean.Order;
import com.wind.data.square.OrderRepaidRequest;
import com.app.qbake.R;
import com.app.qbake.module.square.layout.OrderRepaidLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wind on 2018/10/20.
 */

public class OrderListDelegate extends BaseAdapterDelegate<OrderListDelegate.ViewHolder> {
    private OrderRepaidLayout mOrderRepaidLayout;
    public OrderListDelegate(Activity activity, int layoutRes) {
        super(activity, layoutRes);
        mOrderRepaidLayout=new OrderRepaidLayout(activity);
    }

    @Override
    protected ViewHolder onCreateViewHolder(View itemView) {
        return new ViewHolder(itemView);
    }

    @Override
    protected boolean isForViewType(@NonNull List<DisplayItem> items, int position) {
        //Order order = (Order) items.get(position);

        return true;
    }

    @Override
    protected void onBindViewHolder(@NonNull List<DisplayItem> items, int position, @NonNull RecyclerView.ViewHolder holder, @NonNull List<Object> payloads) {
        final Order order= (Order) items.get(position);
        ViewHolder vh= (ViewHolder) holder;
        ViewInflater.inflateText(vh.tv_name,order.getUser_name());
        ViewInflater.inflateImage(vh.iv,order.getImg_url());
        ViewInflater.inflateText(vh.tv_title,order.getTitle());
        ViewInflater.inflateText(vh.tv_desc,order.getStatus_explain());
        ViewInflater.inflateText(vh.tv_price,"￥"+order.getPrice());
        String status="";
        String op="";
        vh.tv_order_op.setOnClickListener(null);
        switch (order.getOrder_status()){
            case Order.STATUS_PAYFAILED:
                status="支付失败";
                op="重新支付";
                vh.tv_order_op.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        repaid(order.getId(),order.getShop_id());
                    }
                });
                break;
            case Order.STATUS_PENDINGPAY:
                status="待支付";
                op="去支付";
                vh.tv_order_op.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        repaid(order.getId(),order.getShop_id());
                    }
                });
                break;
            case Order.STATUS_PAYED:
                status="已支付";
                op="";
                break;
            case Order.STATUS_PENDINGCOMMENT:
                status="待评论";
                op="去评论";
                break;
            case Order.STATUS_REFUND:
                status="退款";
                op="退款";
                break;
            case Order.STATUS_FINISH:
                status="已结束";
                op="";
                break;
            case Order.STATUS_CLOSED:
                status="已关闭";
                op="";
                break;
        }
        if(TextUtils.isEmpty(op)){
            vh.tv_order_op.setVisibility(View.GONE);
        }else {
            vh.tv_order_op.setVisibility(View.VISIBLE);
            ViewInflater.inflateText(vh.tv_status,op);
        }
        ViewInflater.inflateText(vh.tv_status,status);
    }

    private void repaid(String id, String shop_id) {
        OrderRepaidRequest repaidRequest=new OrderRepaidRequest();
        repaidRequest.setId(id);
        repaidRequest.setShop_id(shop_id);
        mOrderRepaidLayout.repaidOrder(repaidRequest);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_status)
        TextView tv_status;
        @BindView(R.id.iv)
        ImageView iv;
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.tv_price)
        TextView tv_price;
        @BindView(R.id.tv_desc)
        TextView tv_desc;
        @BindView(R.id.tv_order_op)
        TextView tv_order_op;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
